# -*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import json
from pymongo import MongoClient
import pymongo
from decimal import Decimal
city=sys.argv[1]
db_name=sys.argv[2]
client=MongoClient("101.200.122.53",3719)
db=client['admin']
db.authenticate('zhuge', '7UgfAWbUtTKeMVGrSLwsHjB9xGjumnck')
db=client[db_name]
borough_price_file="%s/borough_check" % city
def default(obj):
    if isinstance(obj, Decimal):
        return str(obj)
    raise TypeError
newitem={}
print 'haha'
name_list = {}
subway_cnt=0
cityarea2_cnt=0
cityarea_cnt=0
big_borough=0
empty_name = 0
with open(borough_price_file, 'w') as f:
    print 'count=',db['borough_online'].find().count()
    cnt=0
    for item in db['borough_online'].find(no_cursor_timeout=True):
        cnt=cnt+1
        try:
            name_list = item['borough_byname'] + item['borough_hide']
        except:
            name_list = item['borough_byname']
        if len(name_list)>10:
            f.write('\t'.join(['borough more than 10:',item['borough_name'],str(len(name_list)),'\n']))
            big_borough = big_borough + 1
        try:
            newitem={}
            name = item['borough_name']
            newitem['borough_name']=name
            if not name or name == "":
                empty_name = empty_name + 1
                f.write('\t'.join(['empty borough name:',item['borough_name'],str(item['_id']),'\n']))
            newitem['id']=item['_id']
            try:
                newitem['cityarea_id']=item['cityarea']['cityarea']['cityarea_id']
            except:
                print 'in except cityarea'
                print newitem
                newitem['cityarea_id']= '0'
            try:
                newitem['cityarea2_id']=item['cityarea']['cityarea2'][0]['cityarea2_id']
            except:
                print 'in except cityarea2'
                print newitem
                newitem['cityarea2_id']= '0' 
            if item.get('traffic') and item.get('traffic').get('subway'):
                newitem['subway']= '1'
            else:
                newitem['subway']= '0'
            if not newitem['cityarea_id'] or newitem['cityarea_id'] == '0':
                f.write('\t'.join(['no cityarea:',item['borough_name'],str(item['_id']),'\n']))
            if newitem['subway'] == '1':
                subway_cnt = subway_cnt + 1
            if newitem['cityarea2_id'] and newitem['cityarea2_id'] != '0':
                cityarea2_cnt = cityarea2_cnt + 1
            if newitem['cityarea_id'] and newitem['cityarea_id'] != '0':
                cityarea_cnt = cityarea_cnt + 1            
        except Exception as e:
            print e
    f.write('\t'.join(['subway rate:', str(subway_cnt*100/cnt),'\n']))
    f.write('\t'.join(['cityarea2 rate:', str(cityarea2_cnt*100/cnt),'\n']))
    f.write('\t'.join(['cityarea rate:', str(cityarea_cnt*100/cnt),'\n']))        
    f.write('\t'.join(['big_borough:', str(big_borough),'\n']))
    f.write('\t'.join(['empty_name:', str(empty_name),'\n']))
    cnt = 0 
    no_cityarea_cnt = 0
    for item in db['cityarea2'].find(no_cursor_timeout=True):
        cnt = cnt + 1
        if item['type'] == 0:
            no_cityarea_cnt = no_cityarea_cnt + 1
    f.write('\t'.join(['no_cityarea_cnt:',str(no_cityarea_cnt),'\n']))
    f.close()
